//
//  HZCollectViewController.m
//  HZ_Beautiful_Music
//
//  Created by dllo on 16/3/31.
//  Copyright © 2016年 ui. All rights reserved.
//

#import "HZCollectViewController.h"
#import "WDGDatabaseTool.h"
#import "CSL_CollecCell.h"
#import "CSL_CollecModel.h"
#import <UIImageView+WebCache.h>
#import "CSL_Player_ViewController.h"
@interface HZCollectViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) WDGDatabaseTool *collectDatabase;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *titleArray;
@property (nonatomic, strong) CSL_CollecModel *collecModel;
@property (nonatomic, strong) WDGWhereCondition *wherecondition;
@end

@implementation HZCollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor cyanColor];
    self.navigationItem.title = @"收藏列表";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"Back64"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(BackButtonAction:)];
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.editButtonItem.image = [[UIImage imageNamed:@"bianji"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.editButtonItem.tintColor = [UIColor grayColor];
    
    [self selectDataBase];
    [self CreateTableView];
}

-(void)selectDataBase{
    self.collectDatabase = [WDGDatabaseTool DBManageWithTableName:@"collection"];
    [WDGDatabaseTool openDebugMode:YES];
    [_collectDatabase openDatabase];
    self.titleArray = [NSMutableArray arrayWithCapacity:0];
    for (NSDictionary *Dic in [_collectDatabase selectAllData]){
        _collecModel = [[CSL_CollecModel alloc] initWithDataSource:Dic];
        [_titleArray addObject:_collecModel];
    }
    [_collectDatabase closeDatabase];
}

#pragma mark - 创建TableView
-(void)CreateTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - self.view.bounds.size.height/5.1) style:UITableViewStylePlain];
   _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;//分割线的样式，现在处于无分割线样式
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[CSL_CollecCell class] forCellReuseIdentifier:@"CSL_CollecCellIdentifier"];
    [self.view addSubview:_tableView];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_titleArray.count > 0) {
        return _titleArray.count;
    }
    return 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CSL_CollecCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"CSL_CollecCellIdentifier"];
    _collecModel = _titleArray[indexPath.row];
    [cell.mvImageView sd_setImageWithURL:[NSURL URLWithString:_collecModel.pic_url]];
    cell.titleLabel.text = _collecModel.title;
    cell.nameLabel.text = _collecModel.name;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.view.bounds.size.height/8;
}

//-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
//    NSString *str = [NSString stringWithFormat:@"您共收藏%lu个MV", _titleArray.count];
//    return str;
//}

//-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
//    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 20)];
//    titleView.backgroundColor = [UIColor redColor];
//    
//    UILabel *numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 20)];
//    numberLabel.text = [NSString stringWithFormat:@"共计%lu,", _titleArray.count];
//    return titleView;
//}


//重写系统提供的按钮方法
-(void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
//    让tableView处于编辑/非编辑的状态下
    self.editButtonItem.title = @"";
    [self.tableView setEditing:editing animated:animated];
}

//确定编辑的样式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

//控制是否允许编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    //YES - 允许编辑
    //NO - 不允许编辑
    return YES;
}

//完成编辑
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [_collectDatabase openDatabase];
    _collecModel = _titleArray[indexPath.row];
     self.wherecondition = [WDGWhereCondition conditionWithColumnName:@"name" Operator:@"=" Value:_collecModel.name];
    
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            //删除选中cell对应的数据
            [_collectDatabase deleteDataWithWhereCondition:_wherecondition];
            [_titleArray removeObjectAtIndex:indexPath.row];
            
            [_tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationLeft];
        }
    [_collectDatabase closeDatabase];
}

//控制每一行可以移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

//移除完成执行的方法
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(nonnull NSIndexPath *)sourceIndexPath toIndexPath:(nonnull NSIndexPath *)destinationIndexPath {
//交换数据
    //从数据源取值
    NSString *str = _titleArray[sourceIndexPath.row];
    //删除原位置数据
    [_titleArray removeObjectAtIndex:sourceIndexPath.row];
    //插入到新的位置
    [_titleArray insertObject:str atIndex:destinationIndexPath.row];
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    _collecModel = _titleArray[indexPath.row];
    CSL_Player_ViewController *player_ViewController = [[CSL_Player_ViewController alloc] init];
    player_ViewController.collecMVModel = _collecModel;
    [self presentViewController:player_ViewController animated:YES completion:^{}];
    
    NSNotificationCenter *hiddenCollecButton = [NSNotificationCenter defaultCenter];
    
    [hiddenCollecButton postNotificationName:@"hiddenCollecButton" object:nil userInfo:nil];
}

#pragma mark - 返回按钮的点击事件
-(void)BackButtonAction:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
